package com.trip_memories;

import java.util.List;
import java.util.UUID;

import signedUser.Signed;
import adapters.MyPhotosArrayAdapter;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import dbmodels.Photo;

public class ViewSelectedAlbum extends ListActivity {

	UUID userId;
	UUID albumID;
	Photo[] photos;
	BusinessLogic bL;
	ArrayAdapter<Photo> aa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		userId = Signed.getId();
		bL = AccesBussineLogic.getBussinesLogic(this);
		albumID = UUID.fromString(getIntent().getStringExtra("userid"));
		List<Photo> as = bL.GetPhotosFromAlbum(albumID, userId);
		photos = new Photo[as.size()];

		int count = 0;
		for (Photo photo : as)
			photos[count++] = photo;

		setListAdapter(new MyPhotosArrayAdapter(this, photos));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem itemEdit = menu.add(0, 1, Menu.NONE, R.string.edit);
		MenuItem itemRem = menu.add(0, 2, Menu.NONE, R.string.delete);
		itemEdit.setIcon(android.R.drawable.ic_menu_edit);
		itemRem.setIcon(android.R.drawable.ic_menu_delete);
		return true;
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case 1:
			// showDialog(1);
			break;
		case 2:
			showDialog(2);
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 1: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.no_name_entered);
			builder.setCancelable(true);
			builder.setNegativeButton(android.R.string.cancel,
					new EditOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		case 2: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.for_delete_album);
			builder.setCancelable(true);
			builder.setPositiveButton(android.R.string.ok,
					new OkOnClickListener());
			builder.setNegativeButton(android.R.string.cancel,
					new CancelOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		}
		return super.onCreateDialog(id);
	}

	private final class CancelOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
		}
	}

	private final class OkOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			Log.d("tasl", albumID+"");
			bL.deleteAlbum(albumID);
			finish();
		}
	}

	private final class EditOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			
			finish();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(this, EditPhoto.class);
		
		intent.putExtra("photoId", photos[position].getId().toString());
		startActivity(intent);
		finish();
		
	}

}
